<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/9
 * Time: 17:32
 */

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

class Banner extends Admin
{
    public function index()
    {
        $list = db('banner')->order('sort_order desc')->select();
        return ZBuilder::make('table')
            ->setTableName('goods_brand')
            ->hideCheckbox()
            ->addColumns([
                ['id', 'ID'],
                ['name', '名称'],
                ['img_url', '轮播图','picture'],
                ['link', '链接',''],
                ['sort_order','排序',''],
                ['right_button','操作']
            ])
            ->addTopButton('add')
            ->setRowList($list)
            ->addRightButtons('edit,delete')
            ->fetch();
    }

    public function add()
    {
        if(request()->isPost()){
            $data = input('post.');
            $this->store($data);
        }
        return ZBuilder::make('form')
            ->setPageTitle('添加轮播图')
            ->setUrl(url('add'))
            ->isAjax(false)
            ->addFormItems([
                ['text', 'name', '名称 '],
                ['image', 'img_url','轮播图','规格1：1 小于2MB','','2048'],
                ['text', 'link','链接'],
                ['text','sort_order','排序','','50'],
            ])
            ->fetch();
    }

    public function edit($id = 0)
    {
        if(request()->isPost()){
            $data = input('post.');
            $this->store($data, $id);
        }
        $data = db('banner')->where('id',$id)->find();
        return ZBuilder::make('form')
            ->setPageTitle('编辑轮播图')
            ->setUrl(url('edit'))
            ->isAjax(false)
            ->addHidden('id')
            ->addFormItems([
                ['text', 'name', '名称'],
                ['image', 'img_url','轮播图','规格1：1 小于2MB','','2048'],
                ['text', 'link','链接'],
                ['text','sort_order','排序','','50'],
            ])
            ->setFormData($data)
            ->fetch();
    }

    public function delete($ids = 0)
    {
        $res = db('banner')->where('id',$ids)->delete();
        if($res){
            $this->success('删除成功!','index');
        }else{
            $this->error('删除失败');
        }
    }

    private function store($data, $id = 0)
    {
        if($id == 0){
            $res = db('banner')->insert($data);
        }else{
            $res = db('banner')->where('id',$id)->update($data);
        }

        if($res){
            $this->success('保存成功','index');
        }else{
            $this->error('保存失败');
        }
    }



}
